/*
Copyright (C) 2011  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/

package VMTermClassSettingsDialog;

import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.*;

import GuiTools.*;
import Storage.*;
import Settings.*;

/**
 * A Dialog that prompts the user for two inputs: the name and values.
 * @author Christoph Schnack
 */
class EditElement extends JDialog{
	Language LANG;
	Settings SET;
	
	TribuitFrame parentFrame;
	JDialog eeDialog;
	TribuitTextField nTF;
	JRadioButton tfRB,cbRB;
	JComboBox comboBox;
	JButton delValFromComboBoxBtn;
	Vector<String> oldValues,newValues;
	Vocabulary vocToEdit;
	String tcName,oldName,newName;
	boolean isCanceled = true;
	
	public EditElement(Settings set,TribuitFrame parent,String title){
		super();
		SET = set;
		LANG = SET.getLanguage();
		eeDialog = this;
		parentFrame = parent;
		buildEditElement(title);
		eeDialog.setVisible(true);
	}
	
	public EditElement(Settings set,TribuitFrame parent, String title, String nameArg, Vector<String> valuesArg){
		super();
		SET = set;
		LANG = SET.getLanguage();
		eeDialog = this;
		parentFrame = parent;
		buildEditElement(title);
		oldName = nameArg;
		oldValues = valuesArg;
		nTF.setText(nameArg);
		if(valuesArg!=null){
			comboBox.removeAllItems();
			for(String s : valuesArg){
				comboBox.addItem(s);
			}
			cbRB.doClick();
			delValFromComboBoxBtn.setEnabled(comboBox.getItemCount()>1);
		}
		eeDialog.setVisible(true);
	}
	
	
	
	public void buildEditElement(String title){
		comboBox = new JComboBox();
		comboBox.setPreferredSize(new Dimension(200, comboBox.getPreferredSize().height));
		eeDialog.setTitle(title);
		eeDialog.setModal(true);
		nTF = new TribuitTextField(SET,20);
		tfRB = new JRadioButton("text field");
		cbRB = new JRadioButton("combo box");
		ButtonGroup bg = new ButtonGroup();
		bg.add(tfRB);
		bg.add(cbRB);
		JButton addValToComboBoxBtn = new JButton(LANG.getAdd());
		delValFromComboBoxBtn = new JButton(LANG.getDelete());
		delValFromComboBoxBtn.setEnabled(false);
		addValToComboBoxBtn.addActionListener(new AddValToComboBoxListener());
		delValFromComboBoxBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				int answer = JOptionPane.showConfirmDialog(eeDialog, LANG.getDeleteReally()+ " '"+((String)comboBox.getSelectedItem())+"' ?", LANG.getAttention(), JOptionPane.YES_NO_OPTION);
				if(answer == JOptionPane.YES_OPTION){
					comboBox.removeItem(comboBox.getSelectedItem());
					comboBox.setSelectedIndex(0);
				}
				if(comboBox.getItemCount()==1) delValFromComboBoxBtn.setEnabled(false);
			}
		});
		JPanel edtButtonPanel = new JPanel();
		edtButtonPanel.add(addValToComboBoxBtn);
		edtButtonPanel.add(delValFromComboBoxBtn);
		edtButtonPanel.setBorder(BorderFactory.createTitledBorder(LANG.getEntry()+":"));
		LabelPanel namePanel = new LabelPanel(LANG.getName(),nTF);
		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.insets = new Insets(20,20,20,20);
		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.gridwidth = 2;
		centerPanel.add(namePanel,gbc);
		gbc.gridwidth = 1;
		gbc.gridy=1;
		gbc.insets = new Insets(20,20,0,20);
		centerPanel.add(tfRB,gbc);
		gbc.gridx=1;
		centerPanel.add(cbRB,gbc);
		gbc.gridy=2;
		gbc.insets = new Insets(0,20,20,20);
		centerPanel.add(comboBox,gbc);
		gbc.gridy=3;
		centerPanel.add(edtButtonPanel,gbc);
		JButton applyBtn = new JButton(LANG.getOdConfirm());
		applyBtn.addActionListener(new OkBtnListener());
		JButton cancelBtn = new JButton(LANG.getCancel());
		cancelBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				isCanceled = true;
				eeDialog.dispose();
			}
		});
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(applyBtn);
		buttonPanel.add(cancelBtn);
		buttonPanel.setBackground(GuiTools.BGCOLOR);
		eeDialog.getContentPane().add(BorderLayout.NORTH,new Header(title));
		eeDialog.getContentPane().add(BorderLayout.CENTER, centerPanel);
		eeDialog.getContentPane().add(BorderLayout.SOUTH, buttonPanel);
		eeDialog.pack();
		eeDialog.setLocationRelativeTo(parentFrame);
		tfRB.doClick();
		nTF.grabFocus();
	}
	
	class OkBtnListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			String n = nTF.getText();
			Vector<String> v=null;
			if(n.equals("")||!GuiTools.checkInput(n)){
				GuiTools.showEmptyOrInvalidInputWarning(LANG, eeDialog, LANG.getName());
				return;
			}
			if(cbRB.isSelected()){
				if(comboBox.getItemCount()==0){
					JOptionPane.showMessageDialog(eeDialog,LANG.getComboBoxMustHaveAtLeastOneValue(),LANG.getAttention(),JOptionPane.WARNING_MESSAGE,null);
					return;
				}
				v = new Vector<String>();
				for(int i =0;i<comboBox.getItemCount();i++){
					v.add((String) comboBox.getItemAt(i));
				}
			}
			newValues = v;
			newName = n;
			isCanceled = false;
			eeDialog.dispose();
		}
	}
	
	class AddValToComboBoxListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			Vector<String> args = new Vector<String>();
			String input = "";
			if(comboBox.getItemCount()>0){
				for(int i =0;i<comboBox.getItemCount();i++){
					args.add((String)comboBox.getItemAt(i));
				}
			}
			while(input.equals("") || !GuiTools.checkInput(input) || args.contains(input)){
				input= JOptionPane.showInputDialog(LANG.getAddValue()+" (combo-box)");
				if(input==null) return;
				if(input.equals("") || !GuiTools.checkInput(input)) GuiTools.showEmptyOrInvalidInputWarning(LANG, eeDialog, "input");
				if(args.contains(input)) JOptionPane.showMessageDialog(eeDialog, LANG.getValue()+" '"+input+"' "+LANG.getAlreadyExists(), LANG.getError(), JOptionPane.WARNING_MESSAGE);
			}
			comboBox.addItem(input);
			if(comboBox.getItemCount()>1) delValFromComboBoxBtn.setEnabled(true);
		}
	}
	
	public String getOldName(){
		return oldName;
	}
	
	public String getNewName(){
		return newName;
	}
	
	public Vector<String> getNewValues(){
		return newValues;
	}
	
	public boolean isCanceled(){
		return isCanceled;
	}
}
